package net.muchine.core.support.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.muchine.core.domain.user.Group;
import net.muchine.core.service.user.GroupService;
import net.muchine.core.support.annotation.UniqueGroup;

@Component
public class UniqueGroupValidator implements
		ConstraintValidator<UniqueGroup, Group> {
	
	@Autowired GroupService service;

	public void initialize(UniqueGroup constraintAnnotation) {

	}

	public boolean isValid(Group group, ConstraintValidatorContext context) {
		String name = group.getName();
		if (name == null) return true;
		
		Group result = service.findByName(name);
		if (result != null) return false;
		
		return true;
	}
}
